//
//  MCDietDetailVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/23.
//

import UIKit
import Toast_Swift
import SnapKit
import ObjectMapper
import SwiftyJSON
import Kingfisher
import FSPagerView


class MCDietDetailVc: MCBaseVc,FSPagerViewDataSource,FSPagerViewDelegate {
    
    var zhName:String?
    var labels:Int?
    var ids:String?
    
    let MCDietDetailIdentifier = "MCDietDetailIdentifier"
    
    @IBOutlet weak var headerView: FSPagerView!{
        didSet {
            self.headerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "FSPageViewCellIdentifier")
            self.headerView.itemSize = FSPagerView.automaticSize
        }
    }
    
    @IBOutlet weak var titlesLabel: UILabel!
    
    @IBOutlet weak var labelsLabel: MCPaddingLabel!
    
    @IBOutlet weak var collectionBtn: UIButton!
    
    @IBOutlet weak var jieshaoLabel: UILabel!
    
    @IBOutlet weak var zuofaLabel: UILabel!
    
    
    
    @IBOutlet weak var tuijianCollectionView: UICollectionView!
    
    var imageArray:Array<DietFoodMCFoodDetailList?> = [DietFoodMCFoodDetailList?]()
    
    
    private var dataSource:Array<MCFoodDietRecord?> = [MCFoodDietRecord?]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        let tuiJianCollectionFlowLayout = UICollectionViewFlowLayout.init()
        tuiJianCollectionFlowLayout.scrollDirection = .horizontal
        self.tuijianCollectionView.setCollectionViewLayout(tuiJianCollectionFlowLayout, animated: true)
        self.tuijianCollectionView.delegate = self
        self.tuijianCollectionView.dataSource = self
        self.tuijianCollectionView.showsHorizontalScrollIndicator = false
        self.tuijianCollectionView.register(UINib.init(nibName: "MCDietDetailCell", bundle: Bundle.main), forCellWithReuseIdentifier:MCDietDetailIdentifier)
    
        
        self.headerView.automaticSlidingInterval = 3.0
        self.headerView.isInfinite = true
        self.headerView.delegate = self
        
        
        getLoadData()
        
        recommandList()
        
    }
    
    func recommandList() -> Void {
        NetWorkRequest(MCDietApi.dietFoodListPageList(current: 1, labels: zhName, foodType: labels)) { ResponseModel in
            if ResponseModel.code == 200 {
                let listModel = MCFoodDietModelList(JSONString: ResponseModel.dataString)
                self.dataSource.append(contentsOf: listModel?.records ?? [])
                self.tuijianCollectionView.reloadData()
            }else{
                self.view.makeToast("网络失败",position: .center)
            }
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    

    func getLoadData() {
        NetWorkRequest(MCDietApi.foodDetail(ids:ids ?? "")) { ResponseModel in
            if ResponseModel.code == 200 {
               let responseModel = DietFoodMCDetailModel.init(JSONString: ResponseModel.dataString)
                self.titlesLabel.text = responseModel?.foodName
                self.labelsLabel.text = "≈ \(String(describing: (responseModel?.foodCalorieStandard)!)) kcal"
                self.jieshaoLabel.text = responseModel?.foodIntroduce
                
                switch responseModel?.foodType {
                case "0":
                    self.zuofaLabel.text = "主食"
                case "1":
                    self.zuofaLabel.text = "水果"
                case "2":
                    self.zuofaLabel.text = "蔬菜"
                case "3":
                    self.zuofaLabel.text = "肉蛋奶"
                case "4":
                    self.zuofaLabel.text = "植物油"
                case "5":
                    self.zuofaLabel.text = "豆类坚果"
                case "6":
                    self.zuofaLabel.text = "其他"
                default:
                    self.zuofaLabel.text =  "其他"
                }
                
                self.imageArray.append(contentsOf: responseModel?.foodDetailList ?? [])
                self.headerView.reloadData()
            }else {
                self.view.makeToast("网络失败",position: .center)
            }
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
    
    @IBAction func collectionSender(_ sender: Any) {
        
        
    }
    
    @IBAction func senderShareBtn(_ sender: UIButton) {
        
    }
    
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.imageArray.count
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "FSPageViewCellIdentifier", at: index)
        let imageInfo = self.imageArray[index]
        
        cell.imageView?.kf.setImage(with: URL(string: imageInfo?.fileUrl ?? ""))
        cell.imageView?.contentMode = .scaleAspectFill
        cell.imageView?.clipsToBounds = true
      
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
    }
}

extension MCDietDetailVc:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 132, height: 100)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCDietDetailCell = collectionView.dequeueReusableCell(withReuseIdentifier: MCDietDetailIdentifier, for: indexPath) as! MCDietDetailCell
        let dietModel = self.dataSource[indexPath.row]
        cell.icImageView.kf.setImage(with: URL(string: dietModel?.foodUrl ?? ""))
        cell.titleInfoLabel.text = dietModel?.foodName
        return cell
    }
        
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let dietModel = self.dataSource[indexPath.row]
        let detailVc = MCDietDetailVc.init()
        detailVc.ids = dietModel?.id
        detailVc.zhName = dietModel?.foodName
        detailVc.labels = labels
        self.navigationController?.pushViewController(detailVc, animated: true)
       
    }
    
}
